package com.ttxz.common.http

import com.google.gson.annotations.SerializedName

/**
 * Http请求返回的基类
 *
 * @author ttxz
 * @date 2022/6/15 10:17
 */
open class HttpResponse<T>(
    //此处示例接口中返回的是errorCode，有变化可自修改
        @SerializedName("errorCode")
        open val errorCode: Int = 0,

        @SerializedName("errorMsg")
        open val errorMsg: String? = null,
        @SerializedName("data")
        open val data: T? = null,
        open val errorThrowable: Throwable? = null
) {
    val isSuccess: Boolean
        get() = errorCode == 0
}

data class HttpSuccessResponse<T>(val response: T) : HttpResponse<T>(data = response)

class HttpEmptyResponse<T> : HttpResponse<T>()

/**
 * 接口成功响应，但是数据处理有错误，业务失败
 */
data class HttpDataFailedResponse<T>(override val errorCode: Int, override val errorMsg: String?) :
    HttpResponse<T>(errorCode = errorCode, errorMsg = errorMsg)

data class HttpErrorResponse<T>(val throwable: Throwable) :
    HttpResponse<T>(errorThrowable = throwable)